home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 5 / QRZ Ham Radio Callsign Database - Volume 5.iso / files / amiga / csrc720j.lzh / mbconv10.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-11  |  7.7 KB  |  292 lines

  1. /*
  2.  *  MBCONV10 - 10/1/91A
  3.  *
  4.  *  Copyright (C) 1991
  5.  *  By the CBBS Group.
  6.  *
  7.  *  This program converts the mail datafile from a version 9
  8.  *  mail system to version 10 format.
  9.  *  Version 9 & 10 format requires that the file header information
  10.  *  be included in the first record of the textfile.
  11.  *  Normal textfile data starts at offset 256 in the file.
  12.  *
  13.  *  It is expected that MBCONV10.EXE be placed in the directory
  14.  *  containing the textfiles and be executed from there.
  15.  *  It is also expected that the file MAIL.DAT will be located in the
  16.  *  directory \MB\BBS but if this is not the case, the user will be
  17.  *  given the option of entering the actual directory path information.
  18.  *  Optionally the program can be invoked with the path to the mail.dat
  19.  *  as a command line parameter such as MBCONV10 \HF\BBS.
  20.  *
  21.  */
  22.  
  23. #include "mb.h"
  24. #define ln_hier 64
  25.  
  26. /*  Old message header record. */
  27.  
  28.  
  29. typedef struct omsg_hdr_s
  30. {
  31.   byte ext;           /* Header extension record, or zero                */
  32.   word rn;            /* Record number of this record                    */
  33.   word read;          /* # times the message has been read               */
  34.   word number;        /* Message number                                  */
  35.   word size;          /* Size in bytes                                   */
  36.   char type;          /* Message type                                    */
  37.   byte stat;          /* Message status, see bit definitions above       */
  38.   char to  [ln_call]; /* Destination call                                */
  39.   char from[ln_call]; /* Originator call                                 */
  40.   char bbs [ln_call]; /* Destination BBS, or distribution list           */
  41.   char date[ln_date]; /* Entry date                                      */
  42.   char time[ln_time]; /* Entry time                                      */
  43.   char bid[ln_bid];   /* Bulletin ID, if this is a bulletin              */
  44.   char title[80];     /* Title of message                                */
  45.   char call[16] [ln_call];    /*Distribution calls                       */
  46.   byte flag[16];      /* Distribution flags                              */
  47.   byte count;         /* Number of calls in list                         */
  48.   char fwdc[ln_call]; /* Forwarded callsign                              */
  49. #ifdef MCH_AMIGA
  50.   unsigned char lifetime; /* Lifetime field for compatibility with THEBOX*/
  51.   char unu[5];
  52. #else
  53.   char unu[6];
  54. #endif
  55. } OMSG_HDR;
  56.  
  57. OMSG_HDR  *o_tmmhs;
  58.  
  59. /*
  60.  *  Old mail file header record.
  61.  */
  62.  
  63.  
  64. typedef struct omail_hdr_s
  65. {
  66.   word next;          /* Next record to allocate         */
  67.   word first;         /* First message header record     */
  68.   word last;          /* Last message header record      */
  69.   word next_msg;      /* Next message number             */
  70.   word unt_msg;       /* next_msg at last untangle       */
  71.   byte version;       /* File format version number      */
  72.   word free;          /* Number of records in free chain */
  73.   word count;         /* Number of messages              */
  74.   char date[ln_date]; /* Date of last untangle           */
  75.   char time[ln_time]; /* Time of last untangle           */
  76.   char unu[231];
  77. } OMAIL_HDR;
  78.  
  79. OMAIL_HDR *o_mfhs;
  80.  
  81.  
  82.  
  83. main(argc, argv)
  84. int argc;
  85. char *argv[];
  86. {
  87.  
  88. register word h, rec;
  89. register char c;
  90. char *mpath;
  91. char mbfile[80], mbnfile[80];
  92. char flags[mmesn];
  93. int mfl,  mfln, n;
  94. MAIL_HDR  *mfhs;
  95. MSG_HDR   *tmmhs;
  96.  
  97.  
  98. /*
  99.  *  Allocate space for the mail file records.
  100.  */
  101.  
  102.   mfhs    =  (MAIL_HDR *) malloc(sizeof(MAIL_HDR));
  103.   tmmhs   =  (MSG_HDR *)  malloc(sizeof(MSG_HDR));
  104.   o_tmmhs =  (OMSG_HDR *) malloc(sizeof(OMSG_HDR));
  105.   o_mfhs  =  (OMAIL_HDR *)malloc(sizeof(OMAIL_HDR));
  106.  
  107. #ifndef MCH_AMIGA
  108.   if(argc > 1) mpath = argv[1]; else mpath = "\\mb\\bbs";
  109.   sprintf(mbfile, "%s\\MAIL.DAT", mpath);
  110. #else
  111.   if(argc > 1) mpath = argv[1]; else mpath = "";
  112.   sprintf(mbfile, "%s/MAIL.DAT", mpath);
  113. #endif
  114.  
  115. /*
  116.  *  Open the mail file.
  117.  */
  118.  
  119.   if ((mfl = open(mbfile, O_RDONLY | O_BINARY)) < 0)
  120.      { puts("MAIL.DAT is not in this directory.\nInput directory");
  121.        gets(mpath);
  122. #ifndef MCH_AMIGA
  123.        sprintf(mbfile, "%s\\MAIL.DAT", mpath);
  124. #else
  125.        sprintf(mbfile, "%s/MAIL.DAT", mpath);
  126. #endif
  127.        if ((mfl = open(mbfile, O_RDONLY | O_BINARY)) < 0)
  128.     { puts("MAIL.DAT is not in that directory either. Aborting\n");
  129.       exit(1);
  130.     }
  131.      }
  132.  
  133. /*
  134.  *  Read the mail file header.
  135.  */
  136.  
  137.   read_rec(mfl, 0, (char *)mfhs);
  138.   if (mfhs->version is 10)
  139.    { puts("Mailfile has been converted previously. Aborting\n"); exit(1);}
  140.   read_rec(mfl, 0, (char *)o_mfhs);   
  141.   if (o_mfhs->version < 9 )
  142.    { puts("Mailfile is wrong version. Aborting\n"); exit(1);}
  143.  
  144. /*
  145.  *  Creat the NEW mail file
  146.  */
  147.  
  148.   sprintf(mbnfile, "%s\\MAIL.NEW", mpath);
  149.   if((mfln = open(mbnfile, O_CREAT | O_RDWR | O_BINARY, pmode)) < 0)
  150.    { puts("MAIL.NEW cannot be opened");
  151.      exit (1);
  152.    }
  153.   rec = 0;
  154. /*
  155.  *  Read the mail file records in sequence and
  156.  *  process each corresponding text file.
  157.  */
  158.  
  159.   for (h = o_mfhs->first; h and h < o_mfhs->next; h++)
  160.   {
  161.     read_rec(mfl, h, (char *)o_tmmhs);
  162.     printf("\r%u",h);
  163.  
  164.     fill(tmmhs, '\0', 256);
  165.     fill(flags, ' ', mmesn);
  166.  
  167.     tmmhs->read     = o_tmmhs->read;
  168.     tmmhs->number   = o_tmmhs->number;
  169.     tmmhs->size     = o_tmmhs->size;
  170.     tmmhs->type     = o_tmmhs->type;
  171.     tmmhs->stat     = o_tmmhs->stat;
  172.     strncpy(tmmhs->to, o_tmmhs->to, ln_call);
  173.     strncpy(tmmhs->from, o_tmmhs->from, ln_call);
  174.     strncpy(tmmhs->bbs, o_tmmhs->bbs, ln_call);
  175.     strncpy(tmmhs->date, o_tmmhs->date, ln_date);
  176.     strncpy(tmmhs->time, o_tmmhs->time, ln_time);
  177.     strncpy(tmmhs->bid, o_tmmhs->bid, ln_bid);
  178.     strcpy(tmmhs->title, o_tmmhs->title);
  179.     strncpy(tmmhs->fwdc, o_tmmhs->fwdc, ln_call);
  180. #ifdef MCH_AMIGA
  181.     tmmhs->lifetime = o_tmmhs->lifetime;
  182. #endif
  183.     tmmhs->ext = o_tmmhs->ext;
  184.  
  185.     if (o_tmmhs->count > mmesn) o_tmmhs->count = mmesn;
  186.     if(o_tmmhs->ext is 1)
  187.     {
  188.       for (c = 0; c < o_tmmhs->count; c++)
  189.       {
  190.      strncpy(tmmhs->call[c], o_tmmhs->call[c], ln_call);
  191.      tmmhs->flag[c] = o_tmmhs->flag[c];
  192.      flags[c] = tmmhs->flag[c] + '0';
  193.       }
  194.       tmmhs->count = o_tmmhs->count;
  195.     }
  196.     if (o_tmmhs->ext is 2)
  197.     strncpy(tmmhs->call[0], o_tmmhs->call[0], ln_hier);
  198.  
  199.     ++rec;
  200.     tmmhs->rn = rec;
  201.  
  202.     fill( tmmhs->fwdd, '\0', ln_call);
  203.     fill( tmmhs->fwdl, '\0', ln_call);
  204.     tmmhs->orgnum = 0;
  205.     tmmhs->sp1 = 0;
  206.     tmmhs->sp2 = 0;
  207.     tmmhs->htype = 0;
  208.     tmmhs->fport = 'F';
  209.  
  210.     write_rec(mfln, rec, (char *)tmmhs);
  211.  
  212.   }
  213.  
  214. /*
  215.  *  Write new version number to mailfile and close the file.
  216.  */
  217.  
  218.   printf(" records converted.\n");
  219.   printf("MAIL.DAT had %u messages, MAIL.NEW has %u messages.\n",
  220.      o_mfhs->count, rec);
  221.   mfhs->version = mb_version;
  222.   mfhs->count = mfhs->last = rec;
  223.   mfhs->first = 1;
  224.   mfhs->next = rec + 1;
  225.   mfhs->free = 0;
  226.   mfhs->next_msg = o_mfhs->next_msg;
  227.   mfhs->unt_msg = o_mfhs->unt_msg;
  228.   strncpy(mfhs->date, o_mfhs->date, ln_date);
  229.   strncpy(mfhs->time, o_mfhs->time, ln_time);
  230.   fill(mfhs->unu, '\0', mfhsunu);
  231.  
  232.   write_rec(mfln, 0, (char *)mfhs);
  233.   close(mfl);
  234.   unlink(mbfile);
  235.   printf("Deleting %s\n", mbfile);
  236.   close(mfln);
  237.   printf("Renameing %s to %s\n", mbnfile, mbfile);
  238.   rename(mbnfile, mbfile);
  239.  
  240. }
  241.  
  242. /*
  243.  *  Write random record.
  244.  */
  245.  
  246. write_rec(fid, rec, buffer)
  247. int fid;
  248. int rec;
  249. char buffer[];
  250. {
  251. #ifndef MCH_AMIGA
  252.   long lseek();
  253. #endif
  254.   long offs;
  255.  
  256.   offs = (long)rec * (long)RECSIZE;
  257.   lseek(fid, offs, 0);
  258.   return (write(fid, buffer, RECSIZE) is RECSIZE);
  259. }
  260.  
  261. /*
  262.  *  Read random record.
  263.  */
  264.  
  265. read_rec(fid, rec, buffer)
  266. int fid;
  267. int rec;
  268. char buffer[];
  269. {
  270. #ifndef MCH_AMIGA
  271.   long lseek();
  272. #endif
  273.   long offs;
  274.  
  275.   offs = (long)rec * (long)RECSIZE;
  276.   lseek(fid, offs, 0);
  277.   return (read(fid, buffer, RECSIZE) is RECSIZE);
  278. }
  279.  
  280. /*
  281.  *  Fill some memory with a character.
  282.  */
  283.  
  284. fill(adr, ch, len)
  285. char *adr;
  286. char ch;
  287. int len;
  288. {
  289.   while (len--) *adr++ = ch;
  290. }
  291.  
  292.